package com.learn.sang.xmlparser;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class ParseXmlWithNamespace {
	
	// 获得解析器
	public static XMLStreamReader getStreamReader() {
		String xmlFile = ListUsers.class.getResource("/").getFile()
				+ "../resource/study_use/with_namespace.xml";
		XMLInputFactory factory = XMLInputFactory.newFactory();
		try {
			XMLStreamReader reader = factory
					.createXMLStreamReader(new FileReader(xmlFile));
			return reader;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (XMLStreamException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 列出所有用户名称
	public static void listNames() {
		String namespaceCiteseer = "http://copper.ist.psu.edu/oai/oai_citeseer/";//oai_citeseer
		XMLStreamReader reader = ParseXmlWithNamespace.getStreamReader();
		// 遍历XML文档
		try {
			while (reader.hasNext()) {
				int event = reader.next();
				// 如果是元素的开始
				if (event == XMLStreamConstants.START_ELEMENT) {
					// 
//					System.out.println(reader.getNamespaceURI());
					
					System.out.println("=====:"+reader.getLocalName());
					
					if ("author".equalsIgnoreCase(reader.getLocalName())) {
						System.out.println("*******:"+reader.getNamespaceURI());
						System.out.println("Name:"
								+ reader.getAttributeValue(null, "name"));
					}else if ("title".equalsIgnoreCase(reader.getLocalName())) {
						System.out.println(reader.getElementText().trim());
					}else if("identifier".equalsIgnoreCase(reader.getLocalName())){
						System.out.println("identifier namespace uri:" + reader.getNamespaceURI());
					}
				}
			}
			reader.close();
		} catch (XMLStreamException e) {
			e.printStackTrace();
		}
	}


	
	//获取文件夹下所有xml文件
    public void readFloder(String strWriteXml) {

        File folder = new File(strWriteXml);
        if (folder.exists()) {
            if (folder.isFile()) {
                String s = folder.getName();
                String[] s2 = s.split("\\.");
                if (s2[s2.length - 1].equals("xml")) {
                    // 如果文件类型为xml才存储
                	System.out.println(folder.getAbsoluteFile().toURI());
                }

            } else if (folder.isDirectory()) {
                File[] filelist = folder.listFiles();
                for (int i = 0; i < filelist.length; i++) {
                    readFloder(strWriteXml + "//" + filelist[i].getName());
                    // System.out.println("文件夹"+folder.getName());
                }
            }
        } else {
            System.out.print("文件" + folder.getName() + "不存在");
        }

    }
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ParseXmlWithNamespace.listNames();
	}

}
